package evance.server.common.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import evance.data.model.entity.EvLoginInfo;
import evance.server.common.dao.LoginInfoDAO;

@Service
public class LoginServiceImpl implements LoginService {

	@Autowired(required = true)
	private LoginInfoDAO loginInfoDao;

	@Autowired
	private SecurityRoleService securityRoleService;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		EvLoginInfo info = loginInfoDao.getLoginInfo(username);
		if (info == null) {
			return null;
		}
		return createUserDetails(username, info);
	}

	private User createUserDetails(String username, EvLoginInfo loginInfo) {
		CurrentUserDetails currentUserDetails = new CurrentUserDetails(username, loginInfo.getPassword(), true, true, true, true, securityRoleService.getRoles(loginInfo.getEvCustomer().getId()),
				loginInfo);
		return currentUserDetails;
	}
}
